<?php
session_start();
if ($_SESSION['user'] == "") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href = '../login.php';
    </script>
    <?php
}
//连接数据库
try {
    $db = new PDO('mysql:dbname=books;host=127.0.0.1','root','123456');
    $add= $db->prepare('insert into record (userid,bookid,borrowTime,returnTime,status) values(:phone,:bookid,:borrowTime,:returnTime,:status)');
    $data[':phone']=$_POST['phone'];
    $data[':bookid']=$_POST['bookid'];
    $ri = $data[':borrowTime']=$_POST['borrowTime'];
    $ri = strtotime($ri) + 7*24*60*60;
    $data[':returnTime'] = date('y-m-d',$ri);
    $data[':status']=1;
    if($add->execute($data)){
        $update= $db->prepare('update book set status=1 where bookid=:bookid');
        $re[':bookid']=$_POST['bookid'];
        if($update->execute($re)){
            $updateUser= $db->prepare('update user set status=1 where phone=:phone');
            $resultUser[':phone']=$_POST['phone'];
            if($updateUser->execute($resultUser)){
                echo "<script> location.href='../html/searchBrowwer.php';</script>";
            }
        }
    }else{
        echo "<script> alert('借书失败2!已借出'); location.href='../html/jieshu.php';</script>";
    }
} catch (PDOException $e) {
    echo 'Connection failed';
}
?>

       
      